package com.earnings.okhttputils.utils.car.ui;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.UrlConstant.CarUrl;
import com.earnings.okhttputils.utils.bean.CarListData;
import com.earnings.okhttputils.utils.bean.HomeCarHotData;
import com.earnings.okhttputils.utils.car.http.CarHttp;
import com.earnings.okhttputils.utils.car.http.GodHttpArrayCallback;
import com.earnings.okhttputils.utils.god.GodAdapter.GodArrayList;
import com.earnings.okhttputils.utils.god.GodAdapter.GodBaseAdapter;
import com.earnings.okhttputils.utils.god.GodAdapter.GodViewHolder;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/19 0019.
 */

public class CarListActivity extends GodLeftHandBaseActivity {
    private GodArrayList<CarListData> arrayList = new GodArrayList();
    private GodBaseAdapter adapter;

    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_carlist;
    }

    @Override
    protected void init() {
        String title = getIntent().getStringExtra("title");
        setTitle(title);
        String level = getIntent().getStringExtra("level");
        String imports = getIntent().getStringExtra("import");
        String price = getIntent().getStringExtra("price");
        String stage = getIntent().getStringExtra("stage");
        String id = getIntent().getStringExtra("id");
        Map map = new HashMap();
        if (level != null) {
//        level	int	N	0	级别
//import	int	N	0	国别
//        price	string	N	0	价格区间
//        stage	int	N	0	货款
            map.put("level", level);
            map.put("import", imports);
            map.put("price", price);
            map.put("stage", stage);
        }
        if (id != null) {
            map.put("bid", id);
        }

        adapter = new GodBaseAdapter<HomeCarHotData>(R.layout.item_carlist, arrayList) {
            @Override
            public void onBind(GodViewHolder holder, int position, HomeCarHotData data) {
                Glide.with(CarListActivity.this).load(data.getThumb()).into((ImageView) holder.getView(R.id.img_view));
                holder.setText(R.id.name_tv, data.getTitle());
                holder.setText(R.id.price_tv, "￥" + data.getGprice() + "万");
                holder.setText(R.id.number_tv, data.getSales() + "人购买");
            }

            @Override
            public void OnItemClickListener(View view, int position, HomeCarHotData data) {
                String catid = data.getCatid();
                String id = data.getId();
                mIntent.putExtra("catid", catid);
                mIntent.putExtra("id", id);
                skipActivity(CarDetailsActivity.class);
            }
        };
        setRecyclerViewLayoutManager(R.id.recyclerview, new LinearLayoutManager(this)).setAdapter(adapter);
        http(map);

    }

    private void http(Map map) {
        showProgress();
        CarHttp carHttp = new CarHttp("car", map);
        OkHttpUtils.get().url(CarUrl.CAR_URL).params(carHttp).build().execute(new GodHttpArrayCallback<HomeCarHotData>() {
            @Override
            public void onSuccess(List<HomeCarHotData> datas, String msg, String response) {
                arrayList.addAll(datas);
                dismissProgress();
            }

            @Override
            public void onFail(int code, String msg) {
                dismissProgress();
            }
        });

    }


}
